import Vue from 'vue'
import Vuex from 'vuex'
import { getDiscSongLists, getSongListBySinger, getHotSongLists, checkSongsOpened, getSongsUrl } from "./api/recommend"

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        recommendObj: {},
        recommendLists: [],
        singerPageObj: {},
        singerPageLists: [],
        topSongLists: [],
        topSongObj: [],
        urlLists: JSON.parse(localStorage.getItem("urlLists")) || [],
        currentIndex: -1
    },
    getters: {
        currentIndex(state) {
            return state.currentIndex
        },
        getRecommendLists(state) {
            return state.recommendLists
        },
        getRecommendObj(state) {
            return state.recommendObj
        },
        getSingerPageLists(state) {
            return state.singerPageLists
        },
        getSingerPageObj(state) {
            return state.singerPageObj
        },
        getTopLists(state) {
            return state.topSongLists
        },
        getTopObj(state) {
            return state.topSongObj
        },
        getUrlLists(state) {
            localStorage.setItem("urlLists", JSON.stringify(state.urlLists))
            return state.urlLists
        },
        getUsersSongLists(state) {
            let ids = state.urlLists.map(item => {
                return item.id
            }).join(',')
            return ids
        }
    },
    mutations: {
        recommendListsResult(state, { obj, arr }) {
            state.recommendLists = arr
            state.recommendObj = obj
        },
        singerListsResult(state, { obj, arr }) {
            state.singerPageLists = arr
            state.singerPageObj = obj
        },
        topListsResult(state, { obj, arr }) {
            state.topSongLists = arr
            state.topSongObj = obj
        },
        getSongsUrl(state, url) {
            return url.forEach(item => {
                state.urlLists.push({
                    id: item.id,
                    url: item.url
                })
            })
        },
        changeListIndex(state, newSongs) {
            state.urlLists.splice(state.urlLists.length - 1, 1, newSongs)
            console.log(state.urlLists)
                // newSongs = state.urlLists[state.urlLists.length - 1]
                // state.urlLists[state.urlLists.length - 1] = temp
                // localStorage.setItem('urlLists', JSON.stringify(state.urlLists))
        }
    },
    actions: {
        //获取推荐页中的歌单详情
        getRecommendDetailLists(context, id) {
            getDiscSongLists(id).then(res => {
                context.commit('recommendListsResult', { obj: res.data.playlist, arr: res.data.playlist.tracks })
            });
        },
        //获取歌手页中的歌手单曲详情
        getSingerPageLists(context, id) {
            getSongListBySinger(id).then(res => {
                context.commit('singerListsResult', { obj: res.data.artist, arr: res.data.hotSongs })
            })
        },
        //获取排行榜中的歌单详情
        getTopSongLists(context, id) {
            getHotSongLists(id).then(res => {
                context.commit('topListsResult', { obj: res.data.playlist, arr: res.data.playlist.tracks })
            })
        },

        //检测歌曲是否可用
        checkedSongs(context, id) {
            checkSongsOpened(id).then(res => {
                if (!res.data.success) {
                    alert(res.data.message)
                    return
                } else {
                    //获取歌曲的url
                    let isLists = context.state.urlLists.some(item => {
                        return item.id == id
                    })
                    if (isLists) {
                        let openingSongs = context.state.urlLists.find(item => {
                            return item.id == id
                        })
                        context.commit('changeListIndex', openingSongs)

                        return
                    } {
                        getSongsUrl(id).then(res => {
                            context.commit("getSongsUrl", res.data.data)
                        })
                    }
                }
            }).catch(err => {
                throw err
            })
        }
    }
})